package com.example.shop.imageloader;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.CircleBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;

public class MainActivity extends AppCompatActivity {
    private ImageView mImgCenter;
    private DisplayImageOptions mOptions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mImgCenter = (ImageView) findViewById(R.id.img_center);
        mOptions = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.mipmap.ic_launcher)
                .showImageOnFail(R.mipmap.ic_launcher)
                .showImageOnLoading(R.mipmap.ic_launcher)
//                .resetViewBeforeLoading(true)//每次加载重置我的控件状态
                .cacheOnDisk(true)//是否支持本地缓存
                .imageScaleType(ImageScaleType.EXACTLY)//处理缩略图的,填exactly原图 就可以
                .bitmapConfig(Bitmap.Config.RGB_565)
                .considerExifParams(true)//是否加载exif信息 不做美颜图片不用加？
                //new FadeInBitmapDisplayer(5000)   淡入效果
                //new RoundedBitmapDisplayer(30,10)  圆角  和  内边距
                //new RoundedVignetteBitmapDisplayer(50,20) 设置阴影晕虚
                .displayer(new CircleBitmapDisplayer(Color.RED,5))//特效： 设置圆形图片边宽的颜色和厚度
                .build();
    }

    public void go1(View v) {
        Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();
        ImageLoader.getInstance().displayImage("http://192.168.1.43:8080/xutils/xm.jpg", mImgCenter, mOptions);
    }

    public void go2(View v) {
        Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();
        ImageLoader.getInstance().displayImage("http://192.168.1.43:8080/xutils/xm.jpg", mImgCenter, mOptions, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String s, View view) {

            }

            @Override
            public void onLoadingFailed(String s, View view, FailReason failReason) {

            }

            @Override
            public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                System.out.println("MainActivity.onLoadingComplete "+bitmap.getWidth()+" "+bitmap.getHeight());
            }

            @Override
            public void onLoadingCancelled(String s, View view) {

            }
        });

    }

    public void go3(View v) {
        Toast.makeText(this, "Success", Toast.LENGTH_SHORT).show();
        ImageLoader.getInstance().displayImage("http://192.168.1.43:8080/xutils/xm.jpg", mImgCenter, mOptions, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String s, View view) {

            }

            @Override
            public void onLoadingFailed(String s, View view, FailReason failReason) {

            }

            @Override
            public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                System.out.println("MainActivity.onLoadingComplete " + bitmap.getWidth() + " " + bitmap.getHeight());
            }

            @Override
            public void onLoadingCancelled(String s, View view) {

            }
        }, new ImageLoadingProgressListener() {
            @Override
            public void onProgressUpdate(String s, View view, int i, int i1) {
                //s是url地址   i 加载量  i总量  byte
                System.out.println("s = [" + s + "], view = [" + view + "], i = [" + i + "], i1 = [" + i1 + "]");
            }
        });


    }

    public void go4(View v) {
        Toast.makeText(this, "清除缓存", Toast.LENGTH_SHORT).show();
        ImageLoader.getInstance().clearMemoryCache();
        ImageLoader.getInstance().clearDiskCache();
    }
}
